package com.heliang.dataaccess.db;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;

import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.os.Environment;
import android.util.Log;

import com.example.appxiehui2.R;

/**
 * 
 * @ClassName:     DBManager.java
 * @Description:   数据库操作相关
 * 
 * @author         heliang
 * @version        V1.0  
 * @Date           2013-9-4 上午7:17:52
 */
public class DBManager {
	  private final int BUFFER_SIZE = 1024;
	private SQLiteDatabase sqliteDatabase;
	private Context context;
	private static final String PACKAGE_NAME = "com.appxiehui2";
	private static final String DB_PATH = "/data"+Environment.getDataDirectory().getAbsolutePath()+"/"+PACKAGE_NAME;
	private static final String DB_NAME = "APP_XIEHUI2";
	
	public DBManager(Context context){
		Log.e("cc", "DBManager");
		this.context = context;
	}
	
	/**
	 * 
	 * @Title:       打开数据库连接获得操作数据库的对象 
	 * @Description:  TODO
	 * @param:        @param dbFile
	 * @param:        @return    
	 * @return:       SQLiteDatabase    
	 * @throws
	 */
	private SQLiteDatabase openDatabase(String dbFile){
		Log.e("cc", "open and return");
		Log.i("dbFile",dbFile.toString());
		
		File file = new File(dbFile);
		if(!file.exists()){
			Log.e("cc", "file");
        	InputStream is = context.getResources().openRawResource(R.raw.city);
        	if(is ==null){
        		Log.e("cc", "is null");
        		System.out.println("is null ");
        	}else{
        	}
        	
        	FileOutputStream fos;
			try {
				fos = new FileOutputStream(dbFile);
				if(is==null){
	        		Log.e("cc", "fosnull");
	        		System.out.println("fos null");
	        	}else{
	        	}
	            byte[] buffer = new byte[BUFFER_SIZE];
	            int count = 0;
	            while ((count =is.read(buffer)) > 0) {
	                fos.write(buffer, 0, count);
	            		Log.e("cc", "while");
	            	fos.flush();
	            }
	            
	            System.out.println(dbFile+"----------");
	            sqliteDatabase = SQLiteDatabase.openOrCreateDatabase(dbFile,null);
	            return sqliteDatabase;
	        } catch (FileNotFoundException e) {
	            Log.e("cc", "File not found");
	            e.printStackTrace();
	        } catch (IOException e) {
	            Log.e("cc", "IO exception");
	            e.printStackTrace();
	        } catch (Exception e){
	        	Log.e("cc", "exception "+e.toString());
	        }
		}
	        return null;
	}
	
	/**
	 * 
	 * @Title:        openDaabase 
	 * @Description:  打开数据库连接
	 * @param:            
	 * @return:       void    
	 * @throws
	 */
	public void openDatabase(){
		Log.e("cc", "openDatabase()");
		this.openDatabase(DB_PATH+"/"+DB_NAME);
	}
	
	/**
	 * 
	 * @Title:        getSqliteDatabase 
	 * @Description:  获取数据库连接对象
	 * @param:        @return    
	 * @return:       SQLiteDatabase    
	 * @throws
	 */
	public SQLiteDatabase getSqliteDatabase(){
		Log.e("cc", "getSqliteDatabase()");
    	return this.openDatabase(DB_PATH+"/"+DB_NAME);
	}
	
	/**
	 * 
	 * @Title:        closeDatabase 
	 * @Description:  关闭数据库操作对象
	 * @param:            
	 * @return:       void    
	 * @throws
	 */
	public void closeDatabase(){
		Log.e("cc", "closeDataBase");
		if(sqliteDatabase != null){
			sqliteDatabase.close();
		}
	}
}
